package typeErasure.genericArray;

import java.lang.reflect.Array;

/**
 * Checked:
 * strong typing.
 * GenSet knows explicitly what type of objects it contains
 * (i.e. its constructor was explicitly called with a Class<E> argument,
 * and methods will throw an exception when they are passed arguments that are not of type E
 * @param <E>
 */
public class GenSet<E> {

    private E[] a;

    public GenSet(Class<E> c, int s) {
        // Use Array native method to create array
        // of a type only know at run time
        @SuppressWarnings("unchecked")
        final E[] a = (E[]) Array.newInstance(c, s);
        this.a = a;
    }


    E get(int i) {
        return a[i];
    }


    public static void main(String[] args) {
        GenSet<Integer> integerGenSet = new GenSet<>(Integer.class, 4);
        integerGenSet.a[0] = 1;
        integerGenSet.a[1] = 2;
        integerGenSet.a[2] = 3;
        System.out.println(integerGenSet.get(0) + "," + integerGenSet.get(1) + "," + integerGenSet.get(2));
    }

}
